package redclient.ui;

/*
 *  Copyright 2010 Daniele.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

import b4.advancedgui.menu.AccordionItem;
import b4.advancedgui.menu.AccordionLeafItem;
import b4.advancedgui.menu.AccordionMenu;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;

public final class TimeEntriesForm extends javax.swing.JFrame {

    private AccordionMenu menu3;

    public TimeEntriesForm() {
        /**
         * Changes Look & Feel to Nimbus. And sets icon for JFrame.
         */
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/b4/advancedgui/menu/resources/green_arrow_right.png")));
        } catch (Exception ex) {
            Logger.getLogger(TimeEntriesForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        setSize(600, 600);

        menu3 = new AccordionMenu();
        createSampleMenuStructure(menu3);
        menu3.setBackground(Color.white);
        menu3.setForeground(Color.blue.darker().darker().darker());
        menu3.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, 12));
        menu3.setSelectionColor(Color.lightGray);
        menu3.setLeafHorizontalAlignment(AccordionItem.LEFT);
        ImageIcon icon3 = new ImageIcon(this.getClass().getResource("/b4/advancedgui/menu/resources/blu_arrow_right.png"));
        ImageIcon icon4 = new ImageIcon(this.getClass().getResource("/b4/advancedgui/menu/resources/blu_arrow_down.png"));
        ImageIcon icon5 = new ImageIcon(this.getClass().getResource("/b4/advancedgui/menu/resources/gray_arrow_right.png"));
        ImageIcon icon6 = new ImageIcon(this.getClass().getResource("/b4/advancedgui/menu/resources/green_arrow_right.png"));
        //menu3.setMenuIcons(icon3, icon4);
        //menu3.setLeafIcons("menu2", icon5, icon6);
        setMouseAdapter(menu3);
        cont3.add(menu3);
        //------------------------------------------------------------------------------------------------------------------------

        setExitButton();
    }

    /**
     * Simple bind of an action to a single Menu Leaf Item. This is the most important aspect of menu.
     */
    public void setExitButton() {
        menu3.getLeaf("exit").addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                System.exit(1);
            }
        });
    }

    /**
     * <code>First method to create an AccordionMenu: create a description string of menu tree.</code>
     * It creates a Standard Description String to create an AccordionMenu. This is
     * the full structure of a correct description string. 
     * @return description String.
     */
    public String getSampleMenuDescriptor() {
        String menuDescriptor = ""
                + "Menu One,menu1:"
                + "Sub Menu 1,submenu1.1;"
                + "Sub Menu 2,submenu1.2;"
                + "Exit Button,exit;"
                + "!"
                + "Menu Two,menu2:"
                + "Sub Menu 1,submenu2.1;"
                + "Sub Menu 2,submenu2.2;"
                + "Sub Menu 3,exit;"
                + "!"
                + "Menu Three,menu3:"
                + "Sub Menu 1,submenu3.1;"
                + "Sub Menu 2,submenu3.2;"
                + "Sub Menu 3,exit;"
                + "!"
                + "Menu Four,menu4:"
                + "Sub Menu 1,submenu4.1;"
                + "Sub Menu 2,submenu4.2;"
                + "Sub Menu 3,exit;";
        return menuDescriptor;
    }

    /**
     * <code>Second method to create an AccordionMenu: add manually each menu with its leafs to AccordionMenu.</code>
     * It creates manually a structure like one created before with a description String. First method is better when
     * menu structure is static. Use this method instead if you want to create structure dinamically.
     * @param target Target AccordionMenu to modify.
     */
    public void createSampleMenuStructure(AccordionMenu target) {
        target.addNewMenu("menu1", "Menu One");
        target.addNewLeafTo("menu1", "submenu1.1", "Sub Menu 1");
        target.addNewLeafTo("menu1", "submenu1.2", "Sub Menu 2");
        target.addNewLeafTo("menu1", "exit", "Sub Menu 3");

        target.addNewMenu("menu2", "Menu Two");
        target.addNewLeafTo("menu2", "submenu2.1", "Sub Menu 1");
        target.addNewLeafTo("menu2", "submenu2.2", "Sub Menu 2");
        target.addNewLeafTo("menu2", "submenu2.3", "Sub Menu 3");

        target.addNewMenu("menu3", "Menu Three");
        target.addNewLeafTo("menu3", "submenu3.1", "Sub Menu 1");
        target.addNewLeafTo("menu3", "submenu3.2", "Sub Menu 2");
        target.addNewLeafTo("menu3", "submenu3.3", "Sub Menu 3");

        target.addNewMenu("menu4", "Menu Four");
        target.addNewLeafTo("menu4", "submenu4.1", "Sub Menu 1");
        target.addNewLeafTo("menu4", "submenu4.2", "Sub Menu 2");
        target.addNewLeafTo("menu4", "submenu4.3", "Sub Menu 3");
        target.calculateAvaiableSpace();
    }

    /**
     * Simple example to browse all Leaf of menu. In this case for each leaf it adds a new Mouse Adapter.
     * @param menu Target Menu to modify.
     */
    public void setMouseAdapter(AccordionMenu menu) {
        for (AccordionLeafItem leaf : menu.getAllLeafs()) {
            leaf.addMouseListener(getSimpleMouseAdapter());
        }
    }

    /**
     * Creates a simple MouseAdapter binded to an AccordionItem. On mouse Pressed it writes on a textBox the source of event.
     * @return {@link MouseAdapter} object.
     */
    public MouseAdapter getSimpleMouseAdapter() {
        return new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                if( e.getClickCount() == 2 )
                {
                    AccordionItem item = (AccordionItem) e.getSource();
                    String message = "Source name: " + item.getName() + "; Source Title: " + item.getText();

                    TimeEntriesForm.this.setTitle(message);
                }
            }
        };
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cont3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 204, 204));

        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridLayout(1, 3, 30, 0));

        cont3.setBackground(new java.awt.Color(153, 153, 153));
        cont3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        cont3.setLayout(new javax.swing.BoxLayout(cont3, javax.swing.BoxLayout.LINE_AXIS));
        jPanel1.add(cont3);

        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 678, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(317, 317, 317))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TimeEntriesForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel cont3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
